package tongwentongshu.com.app.dialog;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.IdRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import tongwentongshu.com.app.R;
import tongwentongshu.com.app.contract.CallBackColonyContract;
import tongwentongshu.com.app.utils.AmapUtil;
import tongwentongshu.com.app.utils.NetUtils;
import tongwentongshu.com.app.utils.ToastUtil;
import tongwentongshu.com.app.view.wheel.OnWheelChangedListener;
import tongwentongshu.com.app.view.wheel.StrericWheelAdapter;
import tongwentongshu.com.app.view.wheel.WheelView;


/**
 * Created by XiongBin on 2017/6/28.
 */

public class AlertUtils {
    public static BottomMenuFragment bottomMenuFragment;
    public static AlertDialog alertDialog;

    /**
     * 取消弹窗
     */
    public static void dismiss() {
        // TODO Auto-generated method stub
        if (bottomMenuFragment != null) {
            bottomMenuFragment.dismiss();
        }
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }

    /**
     * 联系对方
     */
    public static AlertDialog initMsgDialog(Context mContext, final CallBackColonyContract.OnMSG onClick) {
        /**
         * 储存选中选中项
         */

        View view = LayoutInflater.from(mContext).inflate(R.layout.menu_call, null);
        TextView call = (TextView) view.findViewById(R.id.tv_call);
        TextView msg = (TextView) view.findViewById(R.id.tv_msg);
        TextView cancel = (TextView) view.findViewById(R.id.tv_cancel);
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.call();
                dismiss();
            }
        });
        msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.msg();
                dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH);

        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    /**
     * 导航对话框
     */
    public static void initNavigationDialog(Activity mContext, final CallBackColonyContract.OnNavigation onClick) {
        bottomMenuFragment = new BottomMenuFragment();

        List<MenuItem> menuItemList = new ArrayList<MenuItem>();
        if (AmapUtil.isGaode()) {
            MenuItem menuItem1 = new MenuItem();
            menuItem1.setText("高德导航");
            menuItem1.setStyle(MenuItem.MenuItemStyle.COMMON);
            menuItem1.setMenuItemOnClickListener(new MenuItemOnClickListener(bottomMenuFragment, menuItem1) {
                @Override
                public void onClickMenuItem(View v, MenuItem menuItem) {
                    onClick.gaode();
                    dismiss();
                }
            });
            menuItemList.add(menuItem1);
        }
        if (AmapUtil.isBaidu()) {
            MenuItem menuItem2 = new MenuItem();
            menuItem2.setText("百度导航");
            menuItem2.setStyle(MenuItem.MenuItemStyle.COMMON);
            menuItem2.setMenuItemOnClickListener(new MenuItemOnClickListener(bottomMenuFragment, menuItem2) {
                @Override
                public void onClickMenuItem(View v, MenuItem menuItem) {
                    onClick.baidu();
                    dismiss();
                }
            });
            menuItemList.add(menuItem2);
        }
        bottomMenuFragment.setMenuItems(menuItemList);

        bottomMenuFragment.show(mContext.getFragmentManager(), "BottomMenuFragment");

    }

    /**
     * 选择图片对话框
     */
    public static void initPhotoAlertDialog(Activity mContext, final CallBackColonyContract.OnPhotoType onClick) {
        bottomMenuFragment = new BottomMenuFragment();

        List<MenuItem> menuItemList = new ArrayList<MenuItem>();
        MenuItem menuItem1 = new MenuItem();
        menuItem1.setText("拍照");
        menuItem1.setStyle(MenuItem.MenuItemStyle.COMMON);
        menuItem1.setMenuItemOnClickListener(new MenuItemOnClickListener(bottomMenuFragment, menuItem1) {
            @Override
            public void onClickMenuItem(View v, MenuItem menuItem) {
                onClick.photoAlbum();
                dismiss();
            }
        });
        MenuItem menuItem2 = new MenuItem();
        menuItem2.setText("相册");
        menuItem2.setMenuItemOnClickListener(new MenuItemOnClickListener(bottomMenuFragment, menuItem2) {
            @Override
            public void onClickMenuItem(View v, MenuItem menuItem) {
                onClick.photograph();
                dismiss();
            }
        });
        menuItemList.add(menuItem1);
        menuItemList.add(menuItem2);

        bottomMenuFragment.setMenuItems(menuItemList);

        bottomMenuFragment.show(mContext.getFragmentManager(), "BottomMenuFragment");

    }


    static int index = 0;

    /**
     * 底部弹出滚动选项菜单
     *
     * @param mContext
     * @param list         数据
     * @param currentindex 当前选中项
     * @param onClick      点击回调
     * @return
     */
    public static AlertDialog initWheelDialog(Context mContext, String[] list, int currentindex, final CallBackColonyContract.OnWheelBack onClick) {
        /**
         * 储存选中选中项
         */

        View view = LayoutInflater.from(mContext).inflate(R.layout.menu_wheel, null);
        WheelView wheelView = (WheelView) view.findViewById(R.id.pop_second_level_wheelView);
        Button btnCancel = (Button) view.findViewById(R.id.pop_second_level_btnCancel);
        Button btnConfirm = (Button) view.findViewById(R.id.pop_second_level_btnConfirm);
        wheelView.setAdapter(new StrericWheelAdapter(list));
        if (currentindex != -1)
            wheelView.setCurrentItem(currentindex);
        wheelView.setCyclic(true);
        wheelView.setInterpolator(new AnticipateOvershootInterpolator());
        wheelView.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                index = newValue;
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.wheelCallBack(index);
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH);

        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    public static Dialog progressDialog;

    /**
     * @param mContext      上下文
     * @param contentString 正文
     * @param leftString    左边文字
     * @param rightString   右边文字
     * @param key           1，左加粗 2，右加粗 0，不加粗
     * @param onClick       点击事件
     * @return
     */
    public static AlertDialog showAgainShare(Context mContext, String contentString, String leftString, String rightString,
                                             int key, final CallBackColonyContract.OnDiyClick onClick) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.menu_again_share, null);
        alertDialog = new AlertDialog.Builder(mContext, R.style.dialog_).create();
        alertDialog.setCancelable(false);
        alertDialog.show();
        TextView content = (TextView) view.findViewById(R.id.tv_content);
        content.setText(contentString);
        TextView left = (TextView) view.findViewById(R.id.tv_left);
        left.setText(leftString);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onClick.left();

            }
        });
        TextView right = (TextView) view.findViewById(R.id.tv_right);
        right.setText(rightString);
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onClick.right();

            }
        });
        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        if (key == 1)
            left.setTypeface(font);
        else if (key == 2)
            right.setTypeface(font);
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH) * 2 / 3;
//        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        Window window = alertDialog.getWindow();
        window.setWindowAnimations(R.style.TRM_ANIM_STYLE);
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);


        return alertDialog;
    }

    public static AlertDialog showInfoShare(Context mContext, String btnString, String contentString, final View.OnClickListener onclick) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.menu_info, null);
        alertDialog = new AlertDialog.Builder(mContext, R.style.dialog_).create();
        alertDialog.show();
        TextView content = (TextView) view.findViewById(R.id.tv_content);
        content.setText(contentString);
        TextView left = (TextView) view.findViewById(R.id.tv_ok);
        left.setText(btnString);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onclick.onClick(v);
                dismiss();
            }
        });
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH) * 2 / 3;
//        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        Window window = alertDialog.getWindow();
        window.setWindowAnimations(R.style.TRM_ANIM_STYLE);
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);


        return alertDialog;
    }

    /**
     * 弹出签到提示
     *
     * @return
     */
    public static AlertDialog showSign(Context mContext, String content) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.menu_sign, null);
        alertDialog = new AlertDialog.Builder(mContext, R.style.dialog_).create();
        alertDialog.show();
        RelativeLayout sign = (RelativeLayout) view.findViewById(R.id.rel_sign);
        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        TextView contentTv = (TextView) view.findViewById(R.id.tv_share);
        contentTv.setText(content);
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH) * 2 / 3;
//        params.width = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
//        int height = (int) NetUtils.getDisplayMetrics((Activity) mContext, NetUtils.DM_WIDTH);

        Window window = alertDialog.getWindow();
//        window.setWindowAnimations(R.style.TRM_ANIM_STYLE);
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);


        return alertDialog;
    }

    public static void showLoding(Context activity) {
        if (progressDialog == null) {
            LayoutInflater inflater = LayoutInflater.from(activity);
            View v = inflater.inflate(R.layout.loading, null);// 得到加载view
            RelativeLayout layout = (RelativeLayout) v
                    .findViewById(R.id.rel_dialog);// 加载布局
            ImageView fireImg = (ImageView) v.findViewById(R.id.iv);
            final AnimationDrawable animDrawable = (AnimationDrawable) fireImg
                    .getBackground();
            fireImg.post(new Runnable() {
                @Override
                public void run() {
                    animDrawable.start();
                }
            });
            progressDialog = new Dialog(activity, R.style.dialog);// 创建自定义样式dialog
            progressDialog.setCancelable(false);// 不可以用“返回键”取消
            progressDialog.setContentView(layout,
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.FILL_PARENT,
                            LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    public static void disMissLoding() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

}
